//
//  OMTAlbumManager.h
//  OMTImagePicker
//
//  Created by yizhaorong on 2017/11/15.
//

#import <Foundation/Foundation.h>
#import "OMTAlbum.h"
#import <pthread.h>
#import <Photos/Photos.h>
#import "OMTImagePickerBuilder.h"

#pragma mark - UIColor宏定义
#define OMTAlbumRGBA(rgbValue, alphaValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0x0000FF))/255.0 \
alpha:alphaValue]

#define OMTAlbumRGB(rgbValue) OMTAlbumRGBA(rgbValue, 1.0)

// 图片间距
FOUNDATION_EXPORT CGFloat const OMTAlbumManagerPadding;
// 相册内容变化通知
FOUNDATION_EXPORT NSString *const OMTAlbumManagerPhotoAssetsChangedNotificaton;

static inline void omt_album_dispatch_async_on_main_queue(void (^block)(void)) {
    if (pthread_main_np()) {
        block();
    } else {
        dispatch_async(dispatch_get_main_queue(), block);
    }
}

// 从右到左语言
static inline BOOL omt_album_is_rtl_language() {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
    
    NSString *currentLanguage = [languages objectAtIndex:0];
    if ([currentLanguage hasPrefix:@"ar"]) return YES;
    return NO;
}

typedef NS_ENUM(NSInteger, OMTAuthorizationStatus) {
    OMTAuthorizationStatusNotDetermined = 0, // User has not yet made a choice with regards to this application
    OMTAuthorizationStatusRestricted,        // This application is not authorized to access photo data.
    // The user cannot change this application’s status, possibly due to active restrictions
    //   such as parental controls being in place.
    OMTAuthorizationStatusDenied,            // User has explicitly denied this application access to photos data.
    OMTAuthorizationStatusAuthorized         // User has authorized this application to access photos data.
};

@interface OMTAlbumManager : NSObject
// item宽高
@property (nonatomic, assign, readonly) CGSize itemSize;
// 选中的图片
@property (nonatomic, strong) NSMutableArray *selectedPhotos;
// 配置
@property (nonatomic, strong) OMTImagePickerBuilder *builder;

+ (instancetype)defaultManager;

+ (OMTAuthorizationStatus)authorizationStatus;

/**
是否已授权

 @return YES 或 NO
 */
+ (BOOL)authorized;

/**
 获取授权

 @param completion 成功回调
 */
+ (void)requestAuthorizationWithCompletion:(void (^)(OMTAuthorizationStatus status))completion;

/**
 获取相册列表

 @param completion 成功回调
 */
- (void)allAlbumsWithCompletion:(void (^)(NSArray<OMTAlbum *> *albums, BOOL finished))completion;

/**
 获取默认图片
 
 @param asset 资源
 @param completion 成功回调
 @return 请求ID
 */
- (PHImageRequestID)defaultPhotoWithAsset:(OMTAsset *)asset
                   completion:(void (^)(UIImage *image, NSData *imageData, NSDictionary *info, BOOL isDegraded, OMTAsset *asset, BOOL finished))completion ;

/**
 获取图片
 
 @param asset 资源
 @param targetWitdh 目标宽度
 @param completion 成功回调
 @return 请求ID
 */
- (PHImageRequestID)photoWithAsset:(OMTAsset *)asset
           targetWitdh:(CGFloat)targetWitdh
            completion:(void (^)(UIImage *image, NSData *imageData, NSDictionary *info, BOOL isDegraded, OMTAsset *asset, BOOL finished))completion;

/**
 获取原图
 
 @param asset 资源
 @param completion 成功回调
 */
- (void)originalPhotoWithAsset:(OMTAsset *)asset completion:(void (^)(UIImage *image, NSData * imageData, NSDictionary *info, OMTAsset *asset, BOOL finished))completion;


/**
 获取相册数据

 @param asset 资源包
 @param completion 回调
 */
- (void)photoDataWithAsset:(OMTAsset *)asset completion:(void (^)(NSData * imageData, OMTAsset *asset, BOOL finished))completion;

/**
 获取相册封面
 
 @param album 相册
 @param completion 封面
 @return 请求ID
 */
- (PHImageRequestID)coverWithAlbum:(OMTAlbum *)album completion:(void (^)(UIImage *image, OMTAlbum *album, BOOL finished))completion;

/**
 刷新相册

 @param album 原相册
 @param completion 成功回调
 */
- (void)refreshAlbum:(OMTAlbum *)album completion:(void (^)(OMTAlbum *album, BOOL finished))completion;

/**
 获取照片数组
 
 @param result 相册
 @param completion 回调
 */
- (void)assetsFromFetchResult:(id)result completion:(void (^)(NSArray<OMTAsset *> *, BOOL finished))completion ;


/**
 取消请求

 @param requestId 请求ID
 */
- (void)cancelRequest:(PHImageRequestID)requestId;


/**
 检查照片大小是否满足最小要求

 @param asset 照片资源
 @return 满足 YES 否则 NO
 */
- (BOOL)isPhotoSelectableWithAsset:(PHAsset *)asset;

@end
